#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
#include "mg_Rotate.h"
/* widgets:
Layer_RGBLight
Background_RGBLight
Container_RGBLight_On
Container_RGBLight_Text
Text_RGBLight_Baifenhao
Text_RGBLight_Liangdu
Container_RGBLight_Button
PopupButton_RGBLight_OFF
PopupButton_RGBLight_Hui
PopupButton_RGBLight_Sun
Meter_RGBLight_Xuanzhuan
Icon_RGBLight_Pointer
CircleProgressBar_RGBLight_Light
Text_RGBLight_PercentNum
Icon_RGBLight_Jingdu
Container_RGBLight_Off
Icon_RGBLight_OFF
PopupButton_RGBLight_ON
Icon_RGBLight_JindutiaoOff
Button_Back_MainMenu
*/
static ITUBackground* Background_RGBLight;
static ITUMeter* Meter_RGBLight_Xuanzhuan;
static ITUContainer* Container_RGBLight_Off;
static ITUContainer* Container_RGBLight_On;
static ITUText* Text_RGBLight_Baifenhao;
static ITUPopupButton* PopupButton_RGBLight_Color;
static ITUPopupButton* PopupButton_RGBLight_W;
static ITUIcon* Icon_RGBLight_Jingdu;
static ITUText* Text_RGBLight_Liangdu;
static uint8_t R, G, B;
static float H, S, V;
static int MeterValue = 0, MeterValue_L = 0;
bool RGBLightOnEnter(ITUWidget* widget, char* param)
{
	if (!Background_RGBLight)//首次加载
	{
		Background_RGBLight = ituSceneFindWidget(&theScene, "Background_RGBLight");
		assert(Background_RGBLight);
		Container_RGBLight_Off = ituSceneFindWidget(&theScene, "Container_RGBLight_Off");
		assert(Container_RGBLight_Off);
		Container_RGBLight_On = ituSceneFindWidget(&theScene, "Container_RGBLight_On");
		assert(Container_RGBLight_On);
		Meter_RGBLight_Xuanzhuan = ituSceneFindWidget(&theScene, "Meter_RGBLight_Xuanzhuan");
		assert(Meter_RGBLight_Xuanzhuan);
		Text_RGBLight_Baifenhao = ituSceneFindWidget(&theScene, "Text_RGBLight_Baifenhao");
		assert(Text_RGBLight_Baifenhao);
		PopupButton_RGBLight_Color = ituSceneFindWidget(&theScene, "PopupButton_RGBLight_Color");
		assert(PopupButton_RGBLight_Color);
		Icon_RGBLight_Jingdu = ituSceneFindWidget(&theScene, "Icon_RGBLight_Jingdu");
		assert(Icon_RGBLight_Jingdu);
		Text_RGBLight_Liangdu = ituSceneFindWidget(&theScene, "Text_RGBLight_Liangdu");
		assert(Text_RGBLight_Liangdu);
		PopupButton_RGBLight_W = ituSceneFindWidget(&theScene, "PopupButton_RGBLight_W");
		assert(PopupButton_RGBLight_W);
	}
	R = TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0];
	G = TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1];
	B = TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[2];
	RGB_2_HSB(R, G, B, &H, &S, &V);
	if (TFT_Device[DisplayValue.TFT_Display_Device].TFTSCEQ.TFT_Device.DeviceType == DEVIC_TYPE_RGBWLIGHT ||
		TFT_Device[DisplayValue.TFT_Display_Device].TFTSCEQ.TFT_Device.DeviceType == DEVIC_TYPE_RGBWLIGHT_KNX)
		ituWidgetSetVisible(PopupButton_RGBLight_W, true);
	else
		ituWidgetSetVisible(PopupButton_RGBLight_W, false);
	//更具参数初始化界面
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status == 1)//设备开
	{
		ituWidgetSetVisible(Container_RGBLight_Off, false);
		ituWidgetSetVisible(Container_RGBLight_On, true);
	}
	else//关闭
	{
		ituWidgetSetVisible(Container_RGBLight_On, false);
		ituWidgetSetVisible(Container_RGBLight_Off, true);
	}
	ituMeterSetValue(Meter_RGBLight_Xuanzhuan, Get_Brightness_By_RGBcolor(R, G, B));
	ituWidgetSetColor(PopupButton_RGBLight_Color, 0xFF, R, G, B);
	ituWidgetSetColor(Icon_RGBLight_Jingdu, 0xFF, R, G, B);

	//对标题进行语言转换
	if (Sys.Language == MG_LANGUAGE_CHS || Sys.Language == MG_LANGUAGE_ENG)
	{
		Text_RGBLight_Liangdu->fontIndex = 0;
		Text_RGBLight_Liangdu->boldSize = 1;
		ituTextSetFontSize(Text_RGBLight_Liangdu, 18);
	}
	else if (Sys.Language == MG_LANGUAGE_ZHTW)
	{
		Text_RGBLight_Liangdu->fontIndex = 8;
		Text_RGBLight_Liangdu->boldSize = 0;
		ituTextSetFontSize(Text_RGBLight_Liangdu, 16);
	}
	//对“%”坐标判断
	if (Meter_RGBLight_Xuanzhuan->value >= 100)
		ituWidgetSetX(Text_RGBLight_Baifenhao, 141);
	else
		ituWidgetSetX(Text_RGBLight_Baifenhao, 123);

	MeterValue = Meter_RGBLight_Xuanzhuan->value;
	MeterValue_L = MeterValue;
	Set_Rotate_Block(30);//旋转步进
    return true;
}

bool RGBLightOnLeave(ITUWidget* widget, char* param)
{
    return true;
}

bool RGBLightOnTimer(ITUWidget* widget, char* param)
{
	static uint32_t SendEndTime = Rotate_SendCmd_Time, SendTime = 0;

	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
	{
		GotoMainMenu();//返回主菜单
		Clear_User_Action();
	}

	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status == 1)//设备开
	{
		if (Action_Cmd.User_Action == Clockwise_Rotation && MeterValue < 100)//顺时针旋转
		{
			MeterValue += Action_Cmd.Action_Times;
			if (MeterValue > 100)MeterValue = 100;
			ituMeterSetValue(Meter_RGBLight_Xuanzhuan, MeterValue);//设置Meter百分比值
			Clear_User_Action();
		}
		else if (Action_Cmd.User_Action == Anticlockwise_Rotation && MeterValue > 0)//逆时针旋转
		{
			MeterValue -= Action_Cmd.Action_Times;
			if (MeterValue < 0)MeterValue = 0;
			ituMeterSetValue(Meter_RGBLight_Xuanzhuan, MeterValue);//设置Meter百分比值
			Clear_User_Action();
		}
	}

	//设备操作
	MeterValue = Meter_RGBLight_Xuanzhuan->value;
	//对“%”坐标判断
	if (Meter_RGBLight_Xuanzhuan->value >= 100)
		ituWidgetSetX(Text_RGBLight_Baifenhao, 141);
	else
		ituWidgetSetX(Text_RGBLight_Baifenhao, 123);
	V = (float)Meter_RGBLight_Xuanzhuan->value / 100;
	HSB_2_RGB(H, S, V, &R, &G, &B);
	ituWidgetSetColor(PopupButton_RGBLight_Color, 0xFF, R, G, B);
	//结束操作设备状态执行
	if (MeterValue == MeterValue_L)
	{
		if (SendEndTime < Rotate_SendCmd_Time)
		{
			SendEndTime++;
			//停止操作0.6s后发送执行报文
			if (SendEndTime == Rotate_SendCmd_Time)
			{
				SendTime = 0;
				RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
				MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
			}
		}
	}
	else
	{
		SendEndTime = 0;
		MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0] = R;
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1] = G;
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[2] = B;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 0x0b;//命令调色
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0] = R;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1] = G;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[2] = B;
		//间隔发送命令
		if (SendTime < Rotating_SendCmd_Time)
			SendTime++;
		if (SendTime == Rotating_SendCmd_Time)//0.495s发送一次命令
		{
			SendTime = 0;
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
		}
	}
	//设备状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status == 1)//设备开
		{
			ituWidgetSetVisible(Container_RGBLight_Off, false);
			ituWidgetSetVisible(Container_RGBLight_On, true);
			R = TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0];
			G = TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1];
			B = TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[2];
			RGB_2_HSB(R, G, B, &H, &S, &V);
			ituMeterSetValue(Meter_RGBLight_Xuanzhuan, Get_Brightness_By_RGBcolor(R, G, B));
			ituWidgetSetColor(PopupButton_RGBLight_Color, 0xFF, R, G, B);
		}
		else//关闭
		{
			ituWidgetSetVisible(Container_RGBLight_On, false);
			ituWidgetSetVisible(Container_RGBLight_Off, true);
		}
		MeterValue = Meter_RGBLight_Xuanzhuan->value;//防止设备循环上报
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
	MeterValue_L = MeterValue;
    return true;
}

bool PopupButton_RGBLight_OFF_Press(ITUWidget* widget, char* param)
{
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 0;//状态关
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 1;//命令关
	printf("Moorgen INFO : Touch to turn off the RGBLight\n");
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

bool PopupButton_RGBLight_ON_Press(ITUWidget* widget, char* param)
{
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 1;//状态开
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 2;//命令开
	printf("Moorgen INFO : Touch to turn on the RGBLight\n");
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

